//index.js
//获取应用实例
var app = getApp()
var wxaSortPicker = require('../../utils/wxaSortPicker/wxaSortPicker.js');
Page({
  data: {
    user_id:'',
    customer_id:'',
    name: '',
  },
  onLoad: function (options) {
    this.setData({
      customer_id: options.id
    })
    var that = this
		    //请求通讯录数据
    wx.request({
      url: app.globalData.httpurl + 'user/list', //和后台交互的地址，默认是json数据交互，由于我的就是json，这里就没有对header进行编写
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
      },
      method: "GET",
      success: function (res) {
        console.log(res.data);
        var datas = res.data.data; //res.data就是从后台接收到的值
        
        var dataItem=[];
        for(var i=0;i<datas.length;i++){
          for(var j=0;j<datas[i].users.length;j++){
            dataItem.push(datas[i].users[j])
          }
        }
        console.log();
        wxaSortPicker.init(dataItem, that);
      },
      fail: function (res) {
        console.log('submit fail');
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })

    wx.setNavigationBarTitle({
      title:'选择业务员'
    })   
  },

  onShow: function () {
    var that = this
  },
  wxaSortPickerItemTap: function(e){
    this.setData({
      showModal: true,
      name: e.currentTarget.dataset.text,
      user_id: e.currentTarget.dataset.value
    })
  },
  hideModal: function () {
    this.setData({
      showModal: false,
      user_id: '',
    })
  },
  /**
    * 弹出框蒙层截断touchmove事件
    */
  preventTouchMove: function () {
  },
  /**
    * 对话框取消按钮点击事件
    */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    var that=this
    //请求分配
    wx.request({
      url: app.globalData.httpurl + 'customer/allot', //和后台交互的地址，默认是json数据交互，由于我的就是json，这里就没有对header进行编写
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        "user_id": this.data.user_id,
        "customer_id": this.data.customer_id
      },
      method: "POST",
      success: function (res) {
        console.log(res.data)
        if(res.data.status == 1){
          that.hideModal();
          wx.showToast({
            title: '分配成功',
          })
          wx.redirectTo({
            url: '/pages/fenpeilist/fenpeilist',
          })
        }else{
          that.hideModal();
          wx.showToast({
            title: res.data.message,
            icon:'none'
          })
        }
      },
      fail: function (res) {
        console.log('submit fail');
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },
  search: function (e) {
    console.log(e.detail.value)
  }
})
